package com.qdsg.ysg.doctor.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;
import com.qdsg.ysg.doctor.util.TimeUtil;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.LogUtil;


/**
 * Description
 * Created by lbj on 2022/4/11 14:33
 */
public class SelectTimeDialog extends BaseDialogFragment {


    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView tv_start_time, tv_end_time;
    private String startDate = "", endDate = "";
    private static final int START_CODE = 0;
    private static final int END_CODE = 1;


    public static SelectTimeDialog newInstance() {
        Bundle args = new Bundle();

        SelectTimeDialog fragment = new SelectTimeDialog();
        fragment.setArguments( args );
        return fragment;
    }


    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_select, container );
        assert getArguments() != null;
        ImageView iv_cancel = view.findViewById( R.id.iv_cancel );
        TextView tv_submit = view.findViewById( R.id.tv_submit );
        tv_start_time = view.findViewById( R.id.tv_start_time );
        tv_end_time = view.findViewById( R.id.tv_end_time );
        iv_cancel.setOnClickListener( v -> {
            if (mListener != null) {
                dismiss();
            }
        } );
        tv_start_time.setOnClickListener( v -> {
            showDatePicker( tv_start_time, START_CODE );
        } );
        tv_end_time.setOnClickListener( v -> {
            showDatePicker( tv_end_time, END_CODE );
        } );
        tv_submit.setOnClickListener( v -> {
            if (UtilString.isEmpty( startDate )) {
                ToastUtil.showToastCenter( mContext, "开始时间不能为空" );
                return;
            }
            LogUtil.e( "时间--" + startDate + "-----" + endDate );
            mListener.onConfirm( startDate, endDate );
//            dismiss();

        } );

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );


    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.BOTTOM );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull( Objects.requireNonNull( getDialog() ).getWindow() ).requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        Objects.requireNonNull( getDialog().getWindow() ).setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }


    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String mStartDate, String mEndDate);

        void onCancel();
    }


    private void showDatePicker(final TextView textView, final int type) {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get( Calendar.YEAR );
        int month = calendar.get( Calendar.MONTH );
        int day = calendar.get( Calendar.DAY_OF_MONTH );
        calendar.set( year, month, day );
        Calendar endDateCalendar = Calendar.getInstance();
        endDateCalendar.set( 2031, 0, 1 );

        TimePickerView pvTime = new TimePickerBuilder( mContext, (date, v) -> {
            textView.setText( TimeUtil.getAllTime( date ) );
            if (type == START_CODE) {
                startDate = TimeUtil.getAllTime( date );
            } else {
                endDate = TimeUtil.getAllTime( date );
            }
        } )
                .setTimeSelectChangeListener( date -> Log.i( "pvTime", "onTimeSelectChanged" ) )
                .setDate( selectedDate )
                .setRangDate( calendar, endDateCalendar )
                .setType( new boolean[]{true, true, true, false, false, false} )
                .isDialog( true ) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener( view -> Log.i( "pvTime", "onCancelClickListener" ) )
                .setLineSpacingMultiplier( 2.0f )
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM );

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams( params );

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations( com.bigkoo.pickerview.R.style.picker_view_slide_anim );//修改动画样式
                dialogWindow.setGravity( Gravity.BOTTOM );//改成Bottom,底部显示
                dialogWindow.setDimAmount( 0.3f );
            }
        }
        pvTime.show();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


}
